package com.example.demo.entity;

import com.example.demo.entity.constants.CommonConstants;

import java.util.HashMap;

/**
 * @author liu ping
 * @description RESTFUL风格 返回信息
 * @date 2018-06-11 11:28:47
 */
public class ResultMsg extends HashMap<String, Object> {

    /**
     * 操作成功
     *
     * @return 返回实例
     */
    public static ResultMsg ok() {
        return ok("操作成功");
    }

    /**
     * 操作失败
     *
     * @return 返回实例
     */
    public static ResultMsg error() {
        return error("操作失败,请稍后重试!");
    }

    /**
     * 构造函数 设置属性
     *
     * @param code    http 状态码
     * @param message 提示信息
     */
    public ResultMsg(int code, String message) {
        put("code", code);
        put("message", message);
    }

    public ResultMsg() {
        super();
    }

    /**
     * 操作成功
     *
     * @return 返回实例
     */
    public static ResultMsg ok(String message) {
        ResultMsg r = new ResultMsg();
        r.put("message", message);
        r.put("code", CommonConstants.HS_SUCCESS);
        r.put("data", "");
        return r;
    }

    /**
     * 操作失败
     *
     * @return 返回实例
     */
    public static ResultMsg error(String message) {
        return getResultMsg(CommonConstants.HS_BUSINESS_ERROR, message);
    }

    /**
     * 操作失败
     *
     * @return 返回实例
     */
    public static ResultMsg getResultMsg(int code, String message) {
        ResultMsg r = new ResultMsg();
        r.put("message", message);
        r.put("code", code);
        r.put("data", "");
        return r;
    }

    /**
     * 未认证
     *
     * @return 返回实例
     */
    public static ResultMsg unauthorized() {
        return getResultMsg(CommonConstants.HS_UNAUTHORIZED, CommonConstants.LOGIN_TIMEOUT);
    }

    /**
     * 没有权限
     *
     * @return 返回实例
     */
    public static ResultMsg notPermission() {
        return getResultMsg(CommonConstants.HS_FORBIDDEN, CommonConstants.NOT_PERMISSION);
    }

    @Override
    public ResultMsg put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * 404 错误
     *
     * @return
     */
    public static ResultMsg notFound() {
        return getResultMsg(404, "请求地址不存在");
    }

    /**
     * 500 错误
     *
     * @return
     */
    public static ResultMsg systemError() {
        return systemError("系统异常");
    }

    /**
     * 500 错误
     *
     * @return
     */
    public static ResultMsg systemError(String message) {
        return getResultMsg(CommonConstants.HS_INTERNAL_SERVER_ERROR, message);
    }

    /**
     * 415 错误
     *
     * @return
     */
    public static ResultMsg unsupportedMediaType() {
        return getResultMsg(415, "content-type 错误");
    }

    /**
     * 405 错误
     *
     * @return
     */
    public static ResultMsg methodNotAllowed() {
        return getResultMsg(405, "Method not allowed");
    }

    /**
     * 400 错误
     *
     * @return
     */
    public static ResultMsg badRequest() {
        return getResultMsg(400, "Bad request");
    }

    /**
     * 成功返回数据
     *
     * @param data
     * @return
     * @author WanChun
     */
    public static ResultMsg successfulData(Object data) {
        ResultMsg r = new ResultMsg();
        r.put("message", "请求成功");
        r.put("code", CommonConstants.HS_SUCCESS);
        r.put("data", data);
        return r;
    }

    /**
     * 成功返回数据
     *
     * @param data
     * @return
     * @author WanChun
     */
    public static ResultMsg successfulData(Object data, String message) {
        ResultMsg r = new ResultMsg();
        r.put("message", message);
        r.put("code", CommonConstants.HS_SUCCESS);
        r.put("code", CommonConstants.HS_SUCCESS);
        r.put("data", data);
        return r;
    }

    public int getCode() {
        return (int) this.get("code");
    }

    public Object getData() {
        return this.get("data");
    }

    public String getMessage() {
        return (String) this.get("message");
    }

}
